﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.UI.WebControls;
using Project.BLL;
using Project.Model;


namespace Project.API.Controllers
{
    public class MenusController : ApiController
    {
        Entities model = new Entities();
        [ApiAuthorize]
        [Route("Api/Menus/GetMenusDtos")]
        public List<MenusDto> Get()
        {

            var userinfo = Request.GetRouteData().Values["auth"] as AuthInfo;

            int roleID = int.Parse(userinfo.Roles);


            var auths = new AuthorityBll().Query().Where(m => m.RId == roleID);

            var list = from a in auths
                       select new MenusDto()
                       {
                           MenuID = a.MId,
                           MenuName = a.Menus.MName,
                           Url = a.Menus.URL,
                           Icon = a.Menus.Icon,
                           ParentID = a.Menus.ParentId,
                           LastTime = a.Menus.LastTime
                       };

            return list.ToList();
        }
        // GET: api/Menus/5
        [Route("Api/Menus/GetMenus")]
        public ResultPageList<MenusDto> Get(int page, int limit, string name = "")
        {
            int count = 0;
            var list = new MenusBll().GetMenusByPage(name, page, limit, out count);
            var menus = new MenusBll().GetMenus().Where(m => m.ParentId == 0);

            var data = from a in list
                       select new MenusDto
                       {
                           MenuID = a.MId,
                           MenuName = a.MName,
                           Url = a.URL,
                           Icon = a.Icon,
                           ParentID = a.ParentId,
                           LastTime = a.LastTime,
                           PName = menus.Where(m => m.MId == a.ParentId).FirstOrDefault() != null ?
                           menus.Where(m => m.MId == a.ParentId).FirstOrDefault().MName : null
                       };

            ResultPageList<MenusDto> result = new ResultPageList<MenusDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = data.ToList();

            return result;
        }
        // GET: api/Menus/5
        [Route("Api/Menus/GetFu")]
        public ResultPageList<Menus> GetFu()
        {
            var list = new MenusBll().GetFu();
            ResultPageList<Menus> result = new ResultPageList<Menus>();
            result.code = 0;
            result.msg = "ok";
            result.count = list.Count;
            result.data = list;

            return result;
        }
        //POST: api/Menus
        public Result Post([FromBody] MenusDto value)
        {

            Menus menu = new Menus();
            menu.MId = value.MenuID;
            menu.MName = value.MenuName;
            menu.ParentId = value.ParentID;
            menu.URL = value.Url;
            menu.Icon = value.Icon;
            menu.LastTime = value.LastTime;


            bool bol = new MenusBll().Add(menu);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "菜单信息添加成功！" : "添加失败";
            return result;
        }
        // PUT: api/Menus/5
        public Result Put([FromBody] MenusDto value)
        {
            Menus menu = new Menus();
            menu.MId = value.MenuID;
            menu.MName = value.MenuName;
            menu.ParentId = value.ParentID;
            menu.URL = value.Url;
            menu.Icon = value.Icon;
            menu.LastTime = value.LastTime;
            bool bol = new MenusBll().Update(menu);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "菜单信息修改成功！" : "修改失败！";
            return result;
        }

        // DELETE: api/Menus/5
        public Result Delete(int id)
        {
            List<Authority> list = model.Authority.Where(a => a.MId == id).ToList();

            model.Authority.RemoveRange(list);

            model.SaveChanges();

            bool bol = new MenusBll().DeleteMenus(id);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "菜单信息删除成功！" : "出错了！";
            return result;
        }

        // DELETE: api/Menus/5
        public Result DeleteAll(int[] id)
        {

            List<Authority> list = model.Authority.Where(a => id.Contains(a.MId)).ToList();

            model.Authority.RemoveRange(list);

            model.SaveChanges();

            bool bol = new MenusBll().DeleteMenusAll(id);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "菜单信息批量删除成功！" : "出错了！";
            return result;
        }
    }
}
